const util = require('../../utils/util.js');
const api = require('../../utils/api.js');
// const regneratorRuntime = getApp().globalData.regneratorRuntime
Page({
  data: {
    banner_list:[],//banner栏list
    images_list:[],//壁纸list
    index:1,//页数
    limit:10,//间隔
    cart_list:[
      {
        "id":"0",
        "rname":"热门推荐",
      },
      {
        "id":"1",
        "rname":"手机壁纸",
      },
      {
        "id":"2",
        "rname":"电脑壁纸",
      },
      {
        "id":"3",
        "rname":"头像",
      },
      {
        "id":"4",
        "rname":"情侣头像",
      },
      {
        "id":"5",
        "rname":"微信头像",
      },
      {
        "id":"6",
        "rname":"QQ头像",
      }
    ],//分类list
    avator_list:[],//头像list
    show_list:[],//展示list
    clickTitle:"热门头像壁纸",
    current:0,
    cart_index:0,
    backTop:false
  },
    //获取滚动条滚动的距离
  onPageScroll(e){
      if(e.scrollTop>200){
        this.setData({
          backTop:true
        })
      }else{
        this.setData({
          backTop:false
        })
      }
  },
  //返回顶部事件
  goTop(){
    wx.pageScrollTo({
      scrollTop:0,
      duration: 500,
    })
  },
  // 切换分类
  changeCurrent(e){
    this.data.show_list = []
    this.data.avator_list = []
    var datas = this.data.show_list
    if (e.target.dataset.index == 0) {
      e.target.dataset.rname = "热门头像壁纸"
    }
    this.setData({
      clickTitle:e.target.dataset.rname
    })
    this.getAvatorData()
    datas = datas.concat(this.data.avator_list)
    this.setData({
      current:e.target.dataset.index,
      cart_index:e.target.dataset.index,
      show_list:datas,
      index:1,
      limit:10
    })
  },
  //banner栏
  getBanner(){
    let that = this;
    util.request(api.ApiBanner).then(function (res) {
      that.setData({
        banner_list:res.data
      })
    })
  },
  //touch start
handleTouchStart: function(e) {    
  this.startTime = e.timeStamp;  
},  
handleTouchEnd: function(e) {    
  this.endTime = e.timeStamp;    
},  

handleClick: function(e) {  
  if (this.endTime - this.startTime < 350) {      
  let userInfo =wx.getStorageSync('userInfo')
  if (!userInfo){
    util.GetUserInfo()
  }else{
    wx.navigateTo({
      url: "/pages/photoInfo/photoInfo?current_index="+e.currentTarget.dataset['index']+"&image_url="+e.currentTarget.dataset['image_url'],
    })
  }   
  }  
},
handleLongPress: function(e) {    
  console.log("长按");  
  },  
  //获取推荐
  getRecommendData(){
    let params = {
      limit:this.data.limit,
      skip:this.data.index
    }
    let that = this;
    util.request(api.ApiHotRecommend,params).then(function (res) {
      var datas = res.data
      for (let index = 0; index < datas.length; index++) {
         datas[index]["wp"] = encodeURIComponent(datas[index]["wp"])
      }
      datas = that.data.images_list.concat(datas)
      that.setData({
        images_list:datas
      })
    })
  },
  //获取头像
  getAvatorData(){
    let that = this;
    util.request(api.ApiAvatorList+"&word="+this.data.clickTitle+"&pn="+this.data.index+"&rn="+this.data.limit,{}).then(function (res) {
      var datas = res.linkData
      for (let index = 0; index < datas.length; index++) {
         datas[index]["wp"] = encodeURIComponent(datas[index]["hoverUrl"])
         datas[index]["img"] = datas[index]["hoverUrl"]
      }
      datas = that.data.show_list.concat(datas)
      that.setData({
        show_list:datas
      })
    })
  },
  // async test(){
  //   let ss = ""
  //   await util.request(api.ApiBanner).then(res=>{
  //     ss = res
  //     console.log(res)
  //   })
  //   console.log(ss)
  // },
  onLoad() {
    // this.getBanner()
    this.getRecommendData()
    this.getAvatorData()
  },
  onReachBottom() {
    this.setData({index:this.data.index + this.data.limit})
    this.getAvatorData()
  },
  getUserProfile(e) {
    // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认，开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
    wx.getUserProfile({
      desc: '展示用户信息', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    })
  },
  //分享
  onShareAppMessage(res){
    return {
      title: "记忆流沙",//自定义转发标题
      path:'/pages/index/index',//当前页面的路径 ，必须是以 / 开头的完整路径
    }
  },
})
